<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="课程ID" prop="courseId">
      <el-input v-model="dataForm.courseId" placeholder="课程ID"></el-input>
    </el-form-item>
    <el-form-item label="课程到天上课时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="课程到天上课时间"></el-input>
    </el-form-item>
    <el-form-item label="课程到天下课时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="课程到天下课时间"></el-input>
    </el-form-item>
    <el-form-item label="上课时间英文，周几" prop="courseWeekEn">
      <el-input v-model="dataForm.courseWeekEn" placeholder="上课时间英文，周几"></el-input>
    </el-form-item>
    <el-form-item label="上课时间中文，周几" prop="courseWeekZh">
      <el-input v-model="dataForm.courseWeekZh" placeholder="上课时间中文，周几"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          courseId: '',
          startTime: '',
          endTime: '',
          courseWeekEn: '',
          courseWeekZh: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          courseId: [
            { required: true, message: '课程ID不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '课程到天上课时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '课程到天下课时间不能为空', trigger: 'blur' }
          ],
          courseWeekEn: [
            { required: true, message: '上课时间英文，周几不能为空', trigger: 'blur' }
          ],
          courseWeekZh: [
            { required: true, message: '上课时间中文，周几不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/coursedate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.courseId = data.courseDate.courseId
                this.dataForm.startTime = data.courseDate.startTime
                this.dataForm.endTime = data.courseDate.endTime
                this.dataForm.courseWeekEn = data.courseDate.courseWeekEn
                this.dataForm.courseWeekZh = data.courseDate.courseWeekZh
                this.dataForm.createTime = data.courseDate.createTime
                this.dataForm.updateTime = data.courseDate.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/coursedate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'courseId': this.dataForm.courseId,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'courseWeekEn': this.dataForm.courseWeekEn,
                'courseWeekZh': this.dataForm.courseWeekZh,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
